package net.xdclass.exception;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.util.JsonData;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author : zhen.xue
 * @Date: 2021-04-02 11:26
 */
@Slf4j
@RestControllerAdvice
public class CustExceptionHandler {

    @ExceptionHandler
    public ResponseEntity<JsonData> handle(Exception e){
        if(e instanceof BizException){
            BizException bizException = (BizException) e;
            log.error("业务异常{}",e.getMessage(),e);
            return new ResponseEntity<>(JsonData.buildCodeAndMsg(bizException.getCode(),bizException.getMessage()),HttpStatus.INTERNAL_SERVER_ERROR);
        }else{
            log.error("系统异常",e);
            return new ResponseEntity<>(JsonData.buildError("未知错误"),HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }
}